package com.winchain.chainbill.bill.biz.transfer.service.impl;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.transfer.rx.BillXfeRevCallbackChdSubscriber;
import com.winchain.chainbill.bill.biz.transfer.rx.BillXfeRevCallbackPrntSubscriber;
import com.winchain.chainbill.bill.biz.transfer.rx.BillXfeRevRollbackPrntSubscriber;
import com.winchain.chainbill.bill.biz.transfer.service.BillXfeEventService;
import com.winchain.chainbill.chain.callback.CallbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("billXfeRevCallbackService")
public class BillXfeRevCallbackServiceImpl implements CallbackService {
	@Autowired
	private BillXfeEventService billXfeEventService;
	@Autowired
	private BillXfeRevCallbackPrntSubscriber billXfeRevCallbackPrntSubscriber;
	@Autowired
	private BillXfeRevCallbackChdSubscriber billXfeRevCallbackChdSubscriber;
	@Autowired
	private BillXfeRevRollbackPrntSubscriber billXfeRevRollbackPrntSubscriber;

	public String getType() {
		return "cb_xfe_rev";
	}

	public void confirm(AcctTxRec tran) {
		String billId = tran.getBizId();
		BillTranEvent<CbXfe> event = this.billXfeEventService.getBillTranEvent(billId, true);
		event.setTran(tran);

		Observable<BillTranEvent<CbXfe>> observers = Observable.just(event);
		observers.subscribe(this.billXfeRevCallbackPrntSubscriber);
		observers.subscribe(this.billXfeRevCallbackChdSubscriber);
	}

	public void cancel(AcctTxRec tran) {
		String billId = tran.getBizId();
		BillTranEvent<CbXfe> event = this.billXfeEventService.getBillTranEvent(billId, true);
		event.setTran(tran);

		Observable<BillTranEvent<CbXfe>> observers = Observable.just(event);
		observers.subscribe(this.billXfeRevRollbackPrntSubscriber);
	}
}
